<template>
  <Dialog v-model="dialogVisible" title="选择农场用户" width="1200px" @close="handleClose">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="用户名称" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-alert
        title="提示：此处只显示具有农场角色的用户"
        type="info"
        :closable="false"
        class="mb-3"
      />
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        max-height="400px"
      >
        <el-table-column label="#" width="55">
          <template #default="{ row }">
            <el-radio :value="row.id" v-model="selectedUserId" @change="handleSelected(row)">
              &nbsp;
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column label="用户ID" align="center" prop="id" width="80" />
        <el-table-column label="用户名称" align="center" prop="username" width="120" />
        <el-table-column label="用户昵称" align="center" prop="nickname" width="120" />
        <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
        <el-table-column label="邮箱" align="center" prop="email" width="180" />
        <el-table-column label="部门" align="center" prop="deptName" width="120" />
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { getUserPage1, type UserVO } from '@/api/system/user'

const dialogVisible = ref(false)
const loading = ref(false)
const list = ref<UserVO[]>([])
const total = ref(0)
const selectedRow = ref<UserVO | null>(null)
const selectedUserId = ref<number>()
const farmRoleId = ref<number>() // 农场角色ID

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: undefined,
  mobile: undefined,
  status: undefined,
  roleId: undefined as number | undefined // 用于筛选特定角色的用户
})

const queryFormRef = ref()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 设置角色ID筛选条件
    queryParams.roleId = farmRoleId.value
    const data = await getUserPage1(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 单选选中时触发 */
const handleSelected = (row: UserVO) => {
  selectedRow.value = row
  // 直接确认选择并关闭弹窗
  emit('confirm', row)
  handleClose()
}

/** 打开弹窗 */
const open = (roleId?: number) => {
  dialogVisible.value = true
  farmRoleId.value = roleId // 设置要筛选的角色ID
  selectedRow.value = null
  selectedUserId.value = undefined
  resetQueryParams()
  getList()
}

/** 重置查询参数 */
const resetQueryParams = () => {
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  queryParams.username = undefined
  queryParams.mobile = undefined
  queryParams.status = undefined
}

/** 关闭弹窗 */
const handleClose = () => {
  dialogVisible.value = false
  selectedRow.value = null
  selectedUserId.value = undefined
}

/** 确定选择 */
const emit = defineEmits(['confirm'])

defineExpose({ open })
</script>
